package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.SocialSecurity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DrawingMapper;
import com.ruoyi.system.domain.Drawing;
import com.ruoyi.system.service.IDrawingService;
import org.springframework.util.CollectionUtils;

import javax.validation.Validator;

/**
 * 抽签Service业务层处理
 * 
 * @author xiongpeng
 * @date 2024-12-29
 */
@Service
public class DrawingServiceImpl implements IDrawingService 
{

    private static final Logger log = LoggerFactory.getLogger(DrawingServiceImpl.class);
    @Autowired
    private DrawingMapper drawingMapper;
    @Autowired
    protected Validator validator;
    /**
     * 查询抽签
     * 
     * @param id 抽签主键
     * @return 抽签
     */
    @Override
    public Drawing selectDrawingById(Long id)
    {
        return drawingMapper.selectDrawingById(id);
    }

    /**
     * 查询抽签列表
     * 
     * @param drawing 抽签
     * @return 抽签
     */
    @Override
    public List<Drawing> selectDrawingList(Drawing drawing)
    {
        return drawingMapper.selectDrawingList(drawing);
    }

    /**
     * 新增抽签
     * 
     * @param drawing 抽签
     * @return 结果
     */
    @Override
    public int insertDrawing(Drawing drawing)
    {
        return drawingMapper.insertDrawing(drawing);
    }

    /**
     * 修改抽签
     * 
     * @param drawing 抽签
     * @return 结果
     */
    @Override
    public int updateDrawing(Drawing drawing)
    {
        drawing.setUpdateTime(DateUtils.getNowDate());
        return drawingMapper.updateDrawing(drawing);
    }

    /**
     * 批量删除抽签
     * 
     * @param ids 需要删除的抽签主键
     * @return 结果
     */
    @Override
    public int deleteDrawingByIds(Long[] ids)
    {
        return drawingMapper.deleteDrawingByIds(ids);
    }

    /**
     * 删除抽签信息
     * 
     * @param id 抽签主键
     * @return 结果
     */
    @Override
    public int deleteDrawingById(Long id)
    {
        return drawingMapper.deleteDrawingById(id);
    }

    @Override
    public String importUser(List<Drawing> userList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入抽检用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Drawing user : userList)
        {
            try
            {
                // 验证是否存在这个用户
                Drawing drawing1 = new Drawing();
                drawing1.setCertId(user.getCertId());
                List<Drawing> drawings = drawingMapper.selectDrawingList(drawing1);
                Drawing u = null;
                if(!CollectionUtils.isEmpty(drawings)){
                    u=drawings.get(0);
                }
                if (StringUtils.isNull(u))
                {
                    BeanValidators.validateWithException(validator, user);
                    drawingMapper.insertDrawing(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 导入成功");
                }
                else if (updateSupport)
                {
                    BeanValidators.validateWithException(validator, user);
                    drawingMapper.updateDrawing(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + user.getUserName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
